<?php
// APIJSON for Webman 自动化测试脚本（PHP 版）
// 运行：php apijson_test.php

function get_json($url, $data) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    $resp = curl_exec($ch);
    $err = curl_error($ch);
    curl_close($ch);
    if ($err) {
        return [null, $err];
    }
    return [json_decode($resp, true), null];
}

$BASE_URL = 'http://127.0.0.1:8787/apijson/req'; // 请根据实际端口调整

$test_cases = [
    // 1 对象方式
    [
        "oldreview" => ["id{}" => [7], "@column" => "id,old_id"],
    ],
    // 2 字段别名
    [
        "oldreview" => ["id{}" => [7], "@column" => "id:iid,old_id"],
    ],
    // 3[] 结构 分页方法
    [
        "[]" => [
            "oldreview" => ["@column" => "id,old_id"],
            "page" => 0, "count" => 10
        ]
    ],
    // 4聚合/分组
    [
        "oldreview" => ["@column" => "old_id,max(old_id):maxoldid", "@group" => "old_id"]
    ],
    // 5.排序
    [
        "oldreview" => ["@column" => "id", "@order" => "id-"]
    ],
    // 6关联加载
    [
        "oldreview" => ["id" => 7, "@column" => "id,old_id", "@with" => ["user:id,name"]]
    ],
    // 7 query1 返回数据数量
    [
        "[]" => ["oldreview" => ["@column" => "id"], "query" => 1],
        "num@" => "/[]/total"
    ],
    // 8 query2 返回数据数量 和 数据
    [
        "[]" => ["oldreview" => ["@column" => "id"], "query" => 2],
        "num@" => "/[]/total"
    ],
    // 9逻辑运算符
    [
        "oldreview" => ["id&{}" => ">=1,<=10", "@column" => "id,old_id"]
    ],
    // 10模糊/正则/JSON 包含
    [
        "oldreview" => ["old_id$" => "%APIJSON%", "@column" => "id,old_id"]
    ],
    // 11
    [
        "oldreview" => ["old_id~" => "^[0-9]+$", "@column" => "id,old_id"]
    ],
    // 12 查询 json
    [
        "oldreview" => ["extra.belongs<>" => 1, "@column" => "id,extra"]
    ],
    // 13 between - 字符串格式
    [
        "oldreview" => ["@between" => "id:1,3|old_id:5,10"]
    ],
    // 14 group having - 字符串格式
    [
        "oldreview" => ["@column" => "old_id,max(old_id):maxoldid", "@group" => "old_id", "@having" => "maxoldid>5"]
    ],
    // 15 with - 字符串格式，多个关联
//    [
//        "oldreview" => ["@with" => "user:id,name|old:id,status"]
//    ],
    // 16 whereRaw - 原生SQL查询
    [
        "users" => [
            "@whereRaw" => [
                "FIND_IN_SET('mature_stable', characteristics) > 0",
                "FIND_IN_SET('reading', hobbies) > 0"
            ],
            "@column" => "id,nickname,characteristics,hobbies"
        ]
    ],
    // 17 whereRaw - 带参数绑定
    [
        "users" => [
            "@whereRaw" => [
                ["FIND_IN_SET(?, characteristics) > 0", ["mature_stable"]],
                ["age >= ?", [25]]
            ],
            "@column" => "id,nickname,characteristics,age"
        ]
    ],
    // 18 whereHas - 点结构支持测试
    [
        "posts" => [
            "@whereHas" => [
                "user.mak" => [
                    "name" => "test",
                    "status" => 1
                ]
            ],
            "@column" => "id,title,content"
        ]
    ],
    // 19 whereHas - 字段点结构支持测试
    [
        "posts" => [
            "@whereHas" => [
                "user" => [
                    "mak.name" => "test",
                    "mak.status" => 1
                ]
            ],
            "@column" => "id,title,content"
        ]
    ],
    // 20 whereHas - 复杂点结构测试
    [
        "posts" => [
            "@whereHas" => [
                "user.mak" => [
                    "@where" => [
                        "profile.age" => 25,
                        "profile.city" => "北京"
                    ],
                    "@whereIn" => [
                        "status" => [1, 2, 3]
                    ]
                ]
            ],
            "@column" => "id,title,content"
        ]
    ],
    // 测试用例 18: FIND_IN_SET 函数（验证 SET 关键词不被误判）
    [
        "users" => [
            "@whereRaw" => "FIND_IN_SET('mature_stable', characteristics) > 0",
            "@column" => "id,nickname,characteristics"
        ]
    ],
    // 测试用例 19: 危险请求测试（应该被阻止）
    [
        "users" => [
            "@whereRaw" => "DROP TABLE users",
            "@column" => "id,nickname"
        ]
    ]
];

function run_tests($BASE_URL, $test_cases) {
    foreach ($test_cases as $i => $case) {
        list($data, $err) = get_json($BASE_URL, $case);
        if ($err) {
            echo "[FAIL] Case ".($i+1).": CURL ERROR: $err\n";
            continue;
        }
        if (is_array($data) && isset($data['result']) && $data['result'] == 1 && isset($data['code']) && $data['code'] == 200) {
            echo "[PASS] Case ".($i+1)."\n";//".json_encode($data, JSON_UNESCAPED_UNICODE)."\n";
        } else {
            echo "[FAIL] Case ".($i+1).": ".json_encode($data, JSON_UNESCAPED_UNICODE)."\n";
        }
    }
}

run_tests($BASE_URL, $test_cases); 